package com.rainbow.controller

import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.cloud.client.ServiceInstance
import org.springframework.cloud.client.discovery.DiscoveryClient
import org.springframework.cloud.client.serviceregistry.Registration
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/serviceB")
class ComputerController {

    private val logger by lazy { LoggerFactory.getLogger(ComputerController::class.java) }

    @Autowired
    private lateinit var client: DiscoveryClient


    @GetMapping
    fun add(@RequestParam a: Int, @RequestParam b: Int): Any {
        val instance: ServiceInstance = client.localServiceInstance


        val r = a + b
        logger.info("/add,host:${instance.host},service_id:${instance.serviceId},result:$r")

        return mapOf("success" to true, "result" to "From Service-B,Result is $r")
    }
}